package com.isoftstone.demo.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.isoftstone.demo.common.OrderVO;
import com.isoftstone.demo.common.utils.CommonUtils;
import com.isoftstone.demo.dao.OrderDao;
import com.isoftstone.demo.pojo.Order;
import com.isoftstone.demo.service.IOrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 订单服务
 **/
@Service
public class OrderServiceImpl implements IOrderService {
    @Resource
    private OrderDao orderDao;

    @Override
    public PageInfo<Order> listOrderByPage(int page, int pageSize) {
        if (pageSize == 0) {
            pageSize = 10;
        }
        PageHelper.startPage(page, pageSize);

        return PageInfo.of(orderDao.listOrder());
    }

    @Override
    public Integer addOrder(Order order) {
        orderDao.insert(order);
        return order.getOid();
    }

    @Override
    public void deleteOrder(Integer id) {
        orderDao.delete(id);
    }

    @Override
    public Order getById(int id) {
        return orderDao.selectById(id);
    }

    @Override
    public void updateOrder(OrderVO vo) {
        Order order = new Order();
        BeanUtils.copyProperties(vo, order);
        order.setOrderdate(CommonUtils.toDate(vo.getStringdate()));
        orderDao.update(order);
    }
}
